import api from '../index';
import type { ResponseData, pageParam } from '../type';
import type { PreviewOrderData, PurchaseGoodsPayData, StoreInfo, CouponsItem } from '.';

export interface OrderParams {
	receivingStatus?: number;
}
interface deputyItem {
	commodityCost: number;
	commodityCount: number;
	commodityFreight: number;
	commodityId: string;
	commodityName: string;
	commoditySpecId: string;
	commoditySpecName: string;
	commodityUrl: string;
	createTime: string;
	dataVerFlag: number;
	deputyId: string;
	deputyMoney: number;
	deputyMoneyCoupon: number;
	deputyMoneyDeduction: number;
	deputyMoneyDis: number;
	deputyMoneyOld: number;
	deputyMoneyPay: number;
	id: number;
	isFlash: 0 | 1;
	orderId: string;
	orderMold: number;
	refundStatus: number;
	source: number;
	status: number;
	storeId: string;
	userId: string;
	storeName:string
}
export interface OrderInfo {
	areaCode: string;
	beginTime: null | string;
	cityCode: string;
	createTime: string; //下单时间
	distributionTime?: string; //配送时间
	id: number;
	invalidTime: string;
	deputys: deputyItem[];
	orderCost: number;
	orderFreight: number; //运费
	orderDeliveryfee?: number; //配送费
	orderId: string;
	orderMold: number;
	orderMoney: number;
	orderMoneyCoupon?: number; //店铺优惠金额
	orderMoneyDeduction: number;
	orderMoneyDis: number;
	orderMoneyPay: number;
	orderPayId: number;
	orderPayName: string;
	orderPayType: string;
	orderRefundStatus: number;
	orderStatus: number;
	orderType: string;
	parentId: string;
	payId: string;
	payTime?: string; //付款时间
	predictTime?: string; //自提时间
	proCode: string;
	productNums: number;
	receivingAddress: string;
	receivingName: string;
	receivingPhone: string;
	receivingStatus: number;
	receivingType: number;
	receivingTime?: string; //完成时间
	remark: null | string;
	refundMoney: number;
	storeId: string;
	storeName: string;
	store: {
		logo: string;
		name: string;
		type:number
	};
	townCode: string;
	updateTime: string;
	userId: string;
	userName: string;
	deliverTime?: string; //发货时间
	expressNumber?: string; //物流单号
	expressName?: string; //物流名称
	expressWebsite?: string; //物流官网地址
	expressType?: 1 | 2;
	verifyCode?: string; //核销码
	virtuallyProPhone?: string;
	orderRefundMsg?: string;
	updateAddressTime?:string
	tranId?:string
}

export interface OrderItem extends OrderInfo {
	
}
// 订单列表
export const getOrderList = (data: OrderParams & pageParam) =>
	api.get<ResponseData<OrderItem[]>>('order/api/main/list', {
		data
	});

// 订单详情
export const getOrderDetail = (id: string) => api.get<ResponseData<OrderInfo>>(`order/api/main/${id}`);
//订单取消退款
export const postCancelRefundOrder = (id: string | number) =>
	api.put(`order/api/main/cancelRefundOrder/${id}`, {
		custom: {
			isLoading: true
		}
	});
// 订单收货
export const postOrderReceiving = (data: { orderId: string }) =>
	api.put('order/api/main/receiving', {
		data,
		custom: {
			isLoading: true,
			showMsg: true,
		}
	});
// 订单批量收货
export const postOrderReceivingBatch = (data: { orderIds: string }) =>
	api.put('order/api/main/receivingBatch', {
		data,
		custom: {
			isLoading: true
		}
	});

// 取消订单
export const postCancelOrder = (data: { orderId: string }) =>
	api.delete('order/api/main/cancelOrder', {
		data,
		custom: {
			isLoading: true,
			showMsg: true,
		}
	});

// 订单待发货申请退款
export const postApplyOrderRefund = (id: string, data: { buyerMsg: string }) =>
	api.post(`order/api/order/refund/applyOrderRefund/${id}`, {
		data,
		custom: {
			isLoading: true,
			showMsg: true,
		}
	});
export interface OrderEvaluateParams {
	descScore: number;
	serviceScore: number;
	deliveryScore: number;
	content: string;
	hasAnonym: number;
	orderDeputyId: string;
	photos?: string;
}
//订单评价
export const postOrderEvaluate = (data: OrderEvaluateParams) =>
	api.post('order/api/main/deputy/orderEvaluate', {
		data,
		custom: {
			showMsg: true,
			isLoading: true
		}
	});
export interface OrderExpress {
	expressWebsite:string
	travel?: {
		expName:string
		number:string
		list: Array<{
			status: string;
			time: string;
		}>;
	};
}
// 待付款订单付款
export const postOrderRepayments = (id: string, data: { payId: number; fromType: string; payword?: string }) =>
	api.post<PurchaseGoodsPayData>(`customer/api/order/repayments/${id}`, {
		data,
		custom: {
			getError:true,
			isLoading: true
		}
	});
// 订单号物流查询
export const getExpressByOrder = (id: string) => api.get<ResponseData<OrderExpress>>(`order/api/main/express/${id}`);
export interface OrderActivityParams {
	data: string;
	deductionStrategy?: string;
	couponId: string;
	couponShareCode: string;
}
export interface OrderCouponsItem extends CouponsItem {
	storeId?: string;
	skuIds: string[];
	productUrls: string[];
	check: boolean;
	moneyAll: number;
}
// 查询一个订单的活动价格
export const getOrderQueryActivity = (data: OrderActivityParams) =>
	api.post<
		ResponseData<{
			order: PreviewOrderData;
			coupon?: OrderCouponsItem[];
			deduction: {
				deductionMode: 'N' | 'Y';
				deductionOpen: 'N' | 'Y';
				deductionType: 'N' | 'Y';
			};
		}>
	>('customer/api/order/query/activity', {
		data,
		custom: {
			isLoading: true
		}
	});

//申请退款的商品信息
export const getRefundGoodsDetail = (id: string) => api.get<ResponseData<deputyItem>>(`order/api/main/deputy/${id}`);

//订单已发货申请退款
export interface ApplyForRefundParams {
	deputyId: string;
	applyType: number;
	buyerMsg: string;
	buyerExplain?: string;
	photoFiles?: string;
}
export const postApplyForRefund = (data: ApplyForRefundParams) =>
	api.post('order/api/order/refund/applyForRefund', {
		data,
		custom: {
			isLoading: true,
			// showMsg: true
		}
	});
//申请退款订单列表
export interface RefundOrderParams {
	orderId?: string;
	commodityName?: string;
	refundSn?: string;
	applyType?: string;
	returnMoneySts?: string;
	params?: {
		beginTime: string;
		endTime: string;
	};
}
export interface RefundOrderItem {
	createTime: string;
	params: {
		deputy: deputyItem;
		store: StoreInfo;
	};
	refundId: number;
	storeId: string;
	orderId: string;
	orderAmount: number;
	orderDeputyId: string;
	refundSn: string;
	flowTradeNo: string;
	outRefundNo?: string;
	payTypeName: string;
	userId: string;
	userPhone: number;
	commodityName: string;
	refundAmount: number;
	applyType: number;
	refundSts: number;
	returnMoneySts: number;
	refundExplain?: string;
	applyTime: string;
	handelTime: string;
	refundTime?: string;
	buyerMsg: string;
	expressName: string;
	expressNo: string;
	receiveTime: string;
	receiveMessage: string;
	receivingName: string;
	receivingPhone: string;
	receivingAddress: string;
}

export const getRefundOrderList = (data: RefundOrderParams & pageParam) =>
	api.get<ResponseData<RefundOrderItem[]>>('order/api/order/refund/list', {
		data
	});
export interface RefundOrderInfo extends RefundOrderItem {}
//申请退款的订单信息
export const getRefundOrderDetail = (id: string) => api.get<ResponseData<RefundOrderInfo>>(`order/api/order/refund/${id}`);
//售后订单取消退款
export const postCancelRefund = (id: number | string) => api.put(`order/api/order/refund/cancelRefund/${id}`,{
	custom: {
		isLoading: true,
		showMsg: true
	}
});
// 提交退货物流单
export const submitExpress = (data: { expressId: string | number; expressNumber: string; refundId: string | number }) =>
	api.post('order/api/order/refund/submitExpress', {
		data
	});

// 查询已提交线下支付历史记录
export const getOfflineInfo = (id: string | number) => api.get(`order/api/pay/offline/detail/${id}`,{
	custom: {
		isLoading: true
	}
});

// 查询抵扣信息
export const getAccountDeduction = (data: any) =>
	api.get('customer/api/account/deduction/commodity', {
		data
	});
export interface AppointmentOrderInfo {
	appointmentMoney:number
	appointmentTime:string
	appointmentTimeStr:string
	appointmentType:number
	areaCode?:string
	cityCode?:string
	createTime:string
	failState:number
	id:number
	moneyAll:number
	payCode:string
	payMoney:number
	proCode?:string
	serviceCount:number
	serviceId:string
	serviceName:string
	servicePhoto:string
	servicePrice:number
	serviceSpec:string
	serviceSpecId:string
	staffId:number
	staffName:string
	staffPhone:string
	state:number
	storeAddressDetail:string
	storeId:string
	storeLogo:string
	storeName:string
	userId:string
	userName:string
	userPhone:string
	writeOffCode:string
	writeOffTime?:string
}

export interface AppointmentOrderItem extends AppointmentOrderInfo {
	
}
// 服务订单列表
export const getAppointmentOrderList = (data: pageParam) =>
	api.get<ResponseData<AppointmentOrderItem[]>>('merchant/customer/appointment/list', {
		data
	});
// 服务订单详情
export const getAppointmentOrderDetail = (data: { id: string }) =>
	api.get('merchant/customer/appointment/detail', {
		data
	});
export interface AppointmentOrderEvaluateParams {
	descScore: number;
	serviceScore: number;
	content: string;
	hasAnonym: 0 | 1;
	orderId: string;
	photos?: string;
}
//服务订单评价
export const postAppointmentOrderEvaluate = (data: AppointmentOrderEvaluateParams) =>
	api.post('merchant/customer/appointment/orderEvaluate', {
		data
	});

//更改订单收货地址
export const updateReceivingAddress = (data: { orderId: string; addressId: number | string }) =>
	api.get('order/api/main/update/receiving/address', {
		data
	});
